<template>
  <div class="category-nav">
    <div class="category-header">
      <div class="header-content">
        <h3 class="category-title">商品分类</h3>
        <span class="subtitle">发现更多好物</span>
      </div>
      <div class="header-decoration">
        <div class="decoration-line"></div>
      </div>
    </div>

    <div class="category-grid">
      <div v-for="category in categories" :key="category.id" class="category-item"
        @click="goToCategory(category.id)">
        <div class="category-icon">
          <el-icon :size="28">
            <component :is="category.icon" />
          </el-icon>
        </div>
        <div class="category-info">
          <div class="category-name">{{ category.name }}</div>
          <div class="category-desc">{{ getCategoryDesc(category.id) }}</div>
        </div>
        <div class="category-arrow">
          <el-icon><ArrowRight /></el-icon>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import request from '@/utils/request'
import type { Category } from '@/types'
import {
  Iphone,
  House,
  Reading,
  Football,
  More,
  Camera,
  Goods,
  ArrowRight
} from '@element-plus/icons-vue'

const router = useRouter()

// 商品分类数据
const categories = ref()
const iconMap: Record<string, any> = {
  1: Iphone,
  2: Goods,
  3: Reading,
  4: House,
  5: Football,
  other: More,
}

// 获取分类描述
const getCategoryDesc = (categoryId: number) => {
  const descMap: Record<number, string> = {
    1: '手机数码',
    2: '服装配饰',
    3: '图书音像',
    4: '家居用品',
    5: '运动户外'
  }
  return descMap[categoryId] || '更多分类'
}

const loadCategories = async () => {
  try {
    // 调用后端分类接口获取分类列表
    const response = await request.get('/category/list', {
      params: {
        pageSize: 20,
      }
    })

    // 检查响应数据结构
    if (response && response.data) {
      // 确保response.data是数组
      const categoryData = Array.isArray(response.data) ? response.data :
        (response.data.data && Array.isArray(response.data.data)) ? response.data.data : []

      if (categoryData.length > 0) {
        // 为每个分类分配icon字段
        categories.value = categoryData.filter((cat: Category) => cat.isUse).map((cat: any) => ({
          ...cat,
          icon: iconMap[cat.id] || More,
        }))
      } else {
        throw new Error('分类数据为空')
      }
    } else {
      throw new Error('分类API响应格式错误')
    }
  } catch (error) {
    console.error('加载分类失败:', error)
    // 如果API不可用，使用默认分类
    categories.value = []
  }
}

// 跳转到分类页面
const goToCategory = (categoryId: number) => {
  router.push({
    path: '/products',
    query: { categoryId: categoryId.toString() }
  })
}

// 初始化
loadCategories()
</script>

<style scoped>
.category-nav {
  background: #fff;
  padding: 40px;
  border-radius: 20px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(255, 107, 53, 0.1);
  position: relative;
  overflow: hidden;
}

.category-nav::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #ff6b35 0%, #f7931e 100%);
}

.category-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 35px;
  position: relative;
}

.header-content {
  flex: 1;
}

.category-title {
  margin: 0 0 8px 0;
  font-size: 28px;
  color: #2c3e50;
  font-weight: 700;
  letter-spacing: -0.5px;
  position: relative;
}

.category-title::after {
  content: '';
  position: absolute;
  bottom: -6px;
  left: 0;
  width: 35px;
  height: 3px;
  background: linear-gradient(90deg, #ff6b35, #f7931e);
  border-radius: 2px;
}

.subtitle {
  color: #7f8c8d;
  font-size: 15px;
  font-weight: 400;
  letter-spacing: -0.1px;
}

.header-decoration {
  flex-shrink: 0;
}

.decoration-line {
  width: 50px;
  height: 2px;
  background: linear-gradient(90deg, transparent, #ff6b35);
  border-radius: 1px;
}

.category-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  max-width: 1000px;
  margin: 0 auto;
}

.category-item {
  display: flex;
  align-items: center;
  padding: 20px;
  border-radius: 16px;
  cursor: pointer;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  background: linear-gradient(145deg, #ffffff 0%, #f8f9fa 100%);
  border: 2px solid transparent;
  position: relative;
  overflow: hidden;
}

.category-item::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 107, 53, 0.1), transparent);
  transition: left 0.6s;
}

.category-item:hover::before {
  left: 100%;
}

.category-item:hover {
  transform: translateY(-6px) scale(1.02);
  box-shadow: 0 16px 40px rgba(255, 107, 53, 0.15);
  border-color: #ff6b35;
  background: linear-gradient(145deg, #ffffff 0%, #fff5f2 100%);
}

.category-icon {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
  background: linear-gradient(135deg, #ff6b35 0%, #f7931e 100%);
  color: white;
  transition: all 0.3s ease;
  box-shadow: 0 6px 20px rgba(255, 107, 53, 0.3);
  flex-shrink: 0;
}

.category-item:hover .category-icon {
  transform: scale(1.15);
  box-shadow: 0 8px 25px rgba(255, 107, 53, 0.4);
}

.category-info {
  flex: 1;
}

.category-name {
  font-size: 16px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 4px;
  letter-spacing: -0.2px;
}

.category-desc {
  font-size: 13px;
  color: #7f8c8d;
  font-weight: 400;
}

.category-arrow {
  color: #cbd5e0;
  transition: all 0.3s ease;
  opacity: 0;
  transform: translateX(-10px);
}

.category-item:hover .category-arrow {
  opacity: 1;
  transform: translateX(0);
  color: #ff6b35;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .category-nav {
    padding: 30px 25px;
    border-radius: 16px;
  }

  .category-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
    margin-bottom: 25px;
  }

  .category-title {
    font-size: 24px;
  }

  .header-decoration {
    align-self: flex-end;
  }

  .category-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 16px;
  }

  .category-item {
    padding: 16px;
    border-radius: 12px;
    flex-direction: column;
    text-align: center;
  }

  .category-icon {
    width: 48px;
    height: 48px;
    margin-right: 0;
    margin-bottom: 12px;
  }

  .category-name {
    font-size: 15px;
  }

  .category-desc {
    font-size: 12px;
  }

  .category-arrow {
    display: none;
  }
}

@media (max-width: 480px) {
  .category-nav {
    padding: 25px 20px;
    border-radius: 12px;
  }

  .category-title {
    font-size: 20px;
  }

  .category-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .category-item {
    padding: 14px;
    flex-direction: row;
    text-align: left;
  }

  .category-icon {
    width: 44px;
    height: 44px;
    margin-right: 12px;
    margin-bottom: 0;
  }

  .category-name {
    font-size: 14px;
  }

  .category-desc {
    font-size: 11px;
  }
}
</style>